package com.wpp;
/*
 * 动态代理第二次练习
 * 买电脑-中介-卖电脑
 *
 * */

import javax.imageio.ImageIO;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface sendComputer {
    void computer();
}

//被代理类
class person implements sendComputer {

    @Override
    public void computer() {
        System.out.println("买一台电脑");
    }

}

//动态代理
class MyProxyTest {
    public static Object getProxy(Object obj) {
        HadlerTest handler = new HadlerTest();
        handler.bind(obj);

        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);

    }


}

class HadlerTest implements InvocationHandler {
    private Object obj;

    public void bind(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("买之前");
        Object invoke = method.invoke(obj);
        System.out.println("买之后");
        return invoke;
    }
}

public class ProxyAndHandler {
    public static void main(String args[]) {
        person person = new person();

        sendComputer proxy = (sendComputer) MyProxyTest.getProxy(person);
        proxy.computer();

    }
}
